﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-05-17 14:16:42
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region  数据访问层
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class LogOperation:ILogOperation
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.ConnectionEjautobaseKey;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(LogID),0)+1
                               FROM [LogOperation]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private LogOperationInfo GetByReader(IDataReader dr)
         {
            LogOperationInfo info = new LogOperationInfo();
            object obj;
            obj=dr["LogID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LogID = (long)obj;
            }
            obj=dr["PlatformID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.PlatformID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["ModuleName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ModuleName = obj.ToString();
            }
            obj=dr["MethodName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MethodName = obj.ToString();
            }
            obj=dr["TargetID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.TargetID = obj.ToString();
            }
            obj=dr["OpContent"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OpContent = obj.ToString();
            }
            obj=dr["OldDataValue"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OldDataValue = obj.ToString();
            }
            obj=dr["NewDataValue"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.NewDataValue = obj.ToString();
            }
            obj=dr["OpUserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OpUserID = (long)obj;
            }
            obj=dr["OpUserName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OpUserName = obj.ToString();
            }
            obj=dr["ServerName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ServerName = obj.ToString();
            }
            obj=dr["ClientIP"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ClientIP = obj.ToString();
            }
            obj=dr["PageUrl"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.PageUrl = obj.ToString();
            }
            obj=dr["OrganizationID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OrganizationID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["CreateDateTime"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreateDateTime = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            return info;
         }
		public long Insert(LogOperationInfo info)
		{
        
        
            string strSql=@"INSERT INTO LogOperation			
			(			
			[PlatformID],
			[ModuleName],
			[MethodName],
			[TargetID],
			[OpContent],
			[OldDataValue],
			[NewDataValue],
			[OpUserID],
			[OpUserName],
			[ServerName],
			[ClientIP],
			[PageUrl],
			[OrganizationID],
			[CreateDateTime]
			)
			 VALUES
			(			
			@PlatformID,
			@ModuleName,
			@MethodName,
			@TargetID,
			@OpContent,
			@OldDataValue,
			@NewDataValue,
			@OpUserID,
			@OpUserName,
			@ServerName,
			@ClientIP,
			@PageUrl,
			@OrganizationID,
			@CreateDateTime
			)
			 SET @LogID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@LogID",DbType.Int64, sizeof(Int64));  
            db.AddInParameter(dbc, "@PlatformID",DbType.Int32, info.PlatformID);
            db.AddInParameter(dbc, "@ModuleName",DbType.String, info.ModuleName);
            db.AddInParameter(dbc, "@MethodName",DbType.String, info.MethodName);
            db.AddInParameter(dbc, "@TargetID",DbType.String, info.TargetID);
            db.AddInParameter(dbc, "@OpContent",DbType.String, info.OpContent);
            db.AddInParameter(dbc, "@OldDataValue",DbType.String, info.OldDataValue);
            db.AddInParameter(dbc, "@NewDataValue",DbType.String, info.NewDataValue);
            db.AddInParameter(dbc, "@OpUserID",DbType.Int64, info.OpUserID);
            db.AddInParameter(dbc, "@OpUserName",DbType.String, info.OpUserName);
            db.AddInParameter(dbc, "@ServerName",DbType.String, info.ServerName);
            db.AddInParameter(dbc, "@ClientIP",DbType.String, info.ClientIP);
            db.AddInParameter(dbc, "@PageUrl",DbType.String, info.PageUrl);
            db.AddInParameter(dbc, "@OrganizationID",DbType.Int32, info.OrganizationID);
            db.AddInParameter(dbc, "@CreateDateTime",DbType.DateTime, info.CreateDateTime.ToNullDbDateTime());

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (long)db.GetParameterValue(dbc,"LogID");
         
		}
		
		
        public void Update(LogOperationInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE LogOperation SET ");
            sbSql.AppendLine("[PlatformID] = @PlatformID,");
                
            sbSql.AppendLine("[ModuleName] = @ModuleName,");
                
            sbSql.AppendLine("[MethodName] = @MethodName,");
                
            sbSql.AppendLine("[TargetID] = @TargetID,");
                
            sbSql.AppendLine("[OpContent] = @OpContent,");
                
            sbSql.AppendLine("[OldDataValue] = @OldDataValue,");
                
            sbSql.AppendLine("[NewDataValue] = @NewDataValue,");
                
            sbSql.AppendLine("[OpUserID] = @OpUserID,");
                
            sbSql.AppendLine("[OpUserName] = @OpUserName,");
                
            sbSql.AppendLine("[ServerName] = @ServerName,");
                
            sbSql.AppendLine("[ClientIP] = @ClientIP,");
                
            sbSql.AppendLine("[PageUrl] = @PageUrl,");
                
            sbSql.AppendLine("[OrganizationID] = @OrganizationID,");
                
            sbSql.AppendLine("[CreateDateTime] = @CreateDateTime");
                
            sbSql.Append(" where  [LogID] = @LogID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@LogID",DbType.Int64, info.LogID);
            db.AddInParameter(dbc, "@PlatformID",DbType.Int32, info.PlatformID);
            db.AddInParameter(dbc, "@ModuleName",DbType.String, info.ModuleName);
            db.AddInParameter(dbc, "@MethodName",DbType.String, info.MethodName);
            db.AddInParameter(dbc, "@TargetID",DbType.String, info.TargetID);
            db.AddInParameter(dbc, "@OpContent",DbType.String, info.OpContent);
            db.AddInParameter(dbc, "@OldDataValue",DbType.String, info.OldDataValue);
            db.AddInParameter(dbc, "@NewDataValue",DbType.String, info.NewDataValue);
            db.AddInParameter(dbc, "@OpUserID",DbType.Int64, info.OpUserID);
            db.AddInParameter(dbc, "@OpUserName",DbType.String, info.OpUserName);
            db.AddInParameter(dbc, "@ServerName",DbType.String, info.ServerName);
            db.AddInParameter(dbc, "@ClientIP",DbType.String, info.ClientIP);
            db.AddInParameter(dbc, "@PageUrl",DbType.String, info.PageUrl);
            db.AddInParameter(dbc, "@OrganizationID",DbType.Int32, info.OrganizationID);
            db.AddInParameter(dbc, "@CreateDateTime",DbType.DateTime, info.CreateDateTime.ToNullDbDateTime());
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(long logID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [LogOperation]  ");
            strSql.Append("WHERE [LogID]=@LogID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@LogID",DbType.Int64, logID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<LogOperationInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<LogOperationInfo> list = new List<LogOperationInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "LogOperation");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    LogOperationInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public LogOperationInfo GetByID(long logID)
		{
            Model.LogOperationInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [LogOperation] ");
            strSql.Append("WHERE [LogID]=@LogID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@LogID",DbType.Int64, logID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}